<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/2
 * Time: 2:36
 */

namespace Lib\Utils;

class Events {
    public static function eventsHandle($user_id, $money) {
        $model = M("Events");
        $list = $model->field("type")->where("state=1")->select();
        if(!empty($list)) {
            for ($i=0; $i<count($list); $i++) {
                if ($list[$i]["type"] == 2) {
                    $model = M("RechargeVoucherConfig");
                    $data = $model->where("money<=$money")->select();
                    if (!empty($data)) {
                        $return_ratio = $data[0]['return_ratio']/100;
                        $voucher_num = intval($return_ratio*$money/$data[0]['money']);
                        Events::addEventsVoucher($user_id, 2, $voucher_num, $data[0]['voucher_range'], $data[0]['day_num'], $data[0]['money'], $data[0]['type'], 1, $data[0]['recharge_money']);
                    }
                } else if ($list[$i]["type"] == 3) {
                    $model = M("MakeRechargeConfig");
                    $data = $model->select();
                    if (!empty($data)) {
                        // 获取推荐用户ID
                        $model = M("User");
                        $p_user = $model->field("pid")->where("id=$user_id")->find();
                        $p_id = $p_user["pid"];
                        for ($j=0;$j<count($data);$j++) {
                            if ($data[$j]["condition_type"] == 1) {
                                // 首次充值送劵
                                $model = M("EventsSendVoucher");
                                $map_1["user_id"] = array("eq",$p_id);
                                $map_1["type"] = array("eq", 2);
                                $map_1["money"] = array("egt", $data[$j]["recharge_money"]);
                                // $map["money"] = $money;
                                $isSend = $model->where($map_1)->count("id");
                                if ($isSend == 0 && $money >=  $data[$j]["money"]) {
                                    Events::addEventsVoucher($p_id, 3, $data[$j]["voucher_num"], $data[$j]['voucher_range'], $data[$j]['day_num'], $data[$j]['money'], $data[$j]['type'], 2, $data[$j]["recharge_money"]);
                                }
                            } else if ($data[$j]["condition_type"] == 2) {
                                // 累计充值送劵
                                $model = M("TradeDetail");
                                $rechargeMoney = $model->where("user_id=$user_id and type=1")->sum("money");
                                $rechargeMoney = $rechargeMoney+$money;
                                if ($rechargeMoney >= $data["recharge_money"]) {
                                    $model = M("EventsSendVoucher");
                                    $map_2["user_id"] = array("eq",$p_id);
                                    $map_2["type"] = array("eq", 3);
                                    $map_2["money"] = array("egt", $data[$j]["recharge_money"]);
                                    // $map_2["money"] = array("eq", "$money");
                                    $eventsSendVoucher = $model->where($map_2)->select();
                                    if (empty($eventsSendVoucher)) {
                                        Events::addEventsVoucher($p_id, 3, $data[$j]["voucher_num"], $data[$j]['voucher_range'], $data[$j]['day_num'], $data[$j]['money'], $data[$j]['type'], 3, $data[$j]["recharge_money"]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private function addEventsVoucher($user_id, $source, $voucher_num, $voucher_range, $day_num, $money, $type, $sendType, $recharge_money) {
        for ($i=0;$i<$voucher_num;$i++) {
            $number = "";
            // 后期需要进行数据库查询判断
            for ($z=0;$z<4;$z++) {
                if($z > 0) {
                    $number = $number."-";
                }
                $number = $number.rand_number();
            }
            $start_date = date('Y-m-d');
            $end_date = date('Y-m-d',strtotime("$start_date +$day_num day"));
            $dataList[] = array(
                'user_id' => $user_id,
                'voucher_range' => $voucher_range,
                'number' => $number,
                'money' => $money,
                'type' => $type,
                'state' => 2,
                'source' => $source,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'datetime' => date('Y-m-d H:i:s')
            );
        }

        if (!empty($dataList)) {
            $model = M('Voucher');
            $model->addAll($dataList);

            $model = M("EventsSendVoucher");
            $eventsSendVoucher["user_id"] = $user_id;
            $eventsSendVoucher["type"] = $sendType;
            $eventsSendVoucher["money"] = $recharge_money;
            $eventsSendVoucher["datetime"] = date('Y-m-d H:i:s');
            $model->add($eventsSendVoucher);
        }
    }
}